﻿<?php if (!defined('SERVER_ROOT')) exit('No direct script access allowed');

class loaidonvi_Model extends Model {

    function __construct(){
        parent::__construct();
    }
    public function qry_all_loaidonvi(){
        return $this->db->ExecuteCursor("BEGIN QLCB.SELECT_LOAI_DON_VI(:CUR); END;",'CUR');
    }
    public function qry_single_loaidonvi($MA_LOAI_DON_VI){
        return $this->db->ExecuteCursor("BEGIN QLCB.SELECT_LOAI_DON_VI_DETAIL(:CUR,'$MA_LOAI_DON_VI'); END;",'CUR');
    }
    public function qry_all_linhvucxuly($MA_LOAI_DON_VI){
        return $this->db->ExecuteCursor("BEGIN QLCB.SELECT_LINH_VUC_XU_LY_LDV(:CUR,'$MA_LOAI_DON_VI'); END;",'CUR');
    }
    public function update_loaidonvi(){
        $P_UPDATE_TYPE = get_post_var('hdn_update_type');
        $P_NGUOI_CAP_NHAT = Session::get(TIEP_DAU_NGU_SESSION.'MA_CAN_BO');
        $P_MA_LOAI_DON_VI = get_post_var('txt_MA_LOAI_DON_VI');
        $P_TEN_LOAI_DON_VI = get_post_var('txt_TEN_LOAI_DON_VI');
        $P_STT_LOAI_DON_VI = get_post_var('txt_STT_LOAI_DON_VI');
        $P_TRANG_THAI = get_post_var('cbx_TRANG_THAI');

        $stmt = $this->db->PrepareSP("BEGIN QLCB.UPDATE_LOAI_DON_VI(:P_VAL,:P_UPDATE_TYPE,:P_NGUOI_CAP_NHAT,:P_MA_LOAI_DON_VI, :P_TEN_LOAI_DON_VI,:P_STT_LOAI_DON_VI,:P_TRANG_THAI);END;");
        $this->db->OutParameter($stmt,$P_VAL,'P_VAL');
        $this->db->InParameter($stmt,$P_UPDATE_TYPE,'P_UPDATE_TYPE');
        $this->db->InParameter($stmt,$P_NGUOI_CAP_NHAT,'P_NGUOI_CAP_NHAT');
        $this->db->InParameter($stmt,$P_MA_LOAI_DON_VI,'P_MA_LOAI_DON_VI');
        $this->db->InParameter($stmt,$P_TEN_LOAI_DON_VI,'P_TEN_LOAI_DON_VI');
        $this->db->InParameter($stmt,$P_STT_LOAI_DON_VI,'P_STT_LOAI_DON_VI');
        $this->db->InParameter($stmt,$P_TRANG_THAI,'P_TRANG_THAI');
        $chaythutuc = $this->db->Execute($stmt);
        //echo '<script>alert("'.$P_VAL.'");</script>';
        if ($chaythutuc){
          if ($P_VAL == 'THANH_CONG'){
              $this->popup_exec_done();
          }elseif($P_VAL == 'TON_TAI_MA'){
              $this->popup_exec_fail(__('Mã loại đơn vị đã tồn tại!'));
              return;
          }elseif($P_VAL == 'TON_TAI_TEN'){
              $this->popup_exec_fail(__('Tên loại đơn vị đã tồn tại!'));
              return;
          }else{
               $this->popup_exec_fail(__('Không thể cập nhật!'));
               return;
          }
        }
    }
    public function delete_loaidonvi(){
        $P_NGUOI_XOA = Session::get(TIEP_DAU_NGU_SESSION.'MA_CAN_BO');
        $P_MA_LOAI_DON_VI = get_post_var('hdn_item_id',0);
        //echo '<script>alert("'.$P_MA_LOAI_DON_VI.'");</script>';
        $stmt = $this->db->PrepareSP("BEGIN QLCB.DELETE_LOAI_DON_VI(:P_VAL,:P_NGUOI_XOA,:P_MA_LOAI_DON_VI);END;");
        $this->db->OutParameter($stmt,$P_VAL,'P_VAL');
        $this->db->InParameter($stmt,$P_NGUOI_XOA,'P_NGUOI_XOA');
        $this->db->InParameter($stmt,$P_MA_LOAI_DON_VI,'P_MA_LOAI_DON_VI');
        $chaythutuc = $this->db->Execute($stmt);
        if ($chaythutuc){
          if ($P_VAL == 'THANH_CONG'){
              $this->exec_done($this->goback_url);
          }else{
               $this->popup_exec_fail(__('Không thể xóa!'));
               return;
          }
        }
    }
     public function update_linhvucxuly(){
        $P_NGUOI_XOA = Session::get(TIEP_DAU_NGU_SESSION.'MA_CAN_BO');
        $P_NGUOI_CAP_NHAT = Session::get(TIEP_DAU_NGU_SESSION.'MA_CAN_BO');
        $P_MA_LOAI_DON_VI  = get_post_var('hdn_item_id',0);
        $stmt = $this->db->PrepareSP("BEGIN QLCB.DELETE_ALL_LDV_CO_XU_LY_LVTT(:P_VAL,:P_NGUOI_XOA,:P_MA_LOAI_DON_VI);END;");
        $this->db->OutParameter($stmt,$P_VAL,'P_VAL');
        $this->db->InParameter($stmt,$P_NGUOI_XOA,'P_NGUOI_XOA');
        $this->db->InParameter($stmt,$P_MA_LOAI_DON_VI,'P_MA_LOAI_DON_VI');
        $chaythutuc = $this->db->Execute($stmt);
        // echo '<script>alert("'.$P_VAL.'");</script>';
        if ($chaythutuc){
          if ($P_VAL == 'THANH_CONG'){
              $v_phan_lvxl       = get_post_var('hdn_phan_lvxl','');
              $arr_phan_lvxl = explode(',', $v_phan_lvxl);
              foreach ($arr_phan_lvxl as $P_MA_LINH_VUC_THU_TUC){
                  if ( $P_MA_LINH_VUC_THU_TUC != NULL && $P_MA_LINH_VUC_THU_TUC != ''){
                      $stmt = $this->db->PrepareSP("BEGIN QLCB.INSERT_LDV_CO_XU_LY_LVTT(:P_VAL,:P_NGUOI_CAP_NHAT,:P_MA_LOAI_DON_VI,:P_MA_LINH_VUC_THU_TUC);END;");
                      $this->db->OutParameter($stmt,$P_VAL,'P_VAL');
                      $this->db->InParameter($stmt,$P_NGUOI_CAP_NHAT,'P_NGUOI_CAP_NHAT');
                      $this->db->InParameter($stmt,$P_MA_LOAI_DON_VI,'P_MA_LOAI_DON_VI');
                      $this->db->InParameter($stmt,$P_MA_LINH_VUC_THU_TUC,'P_MA_LINH_VUC_THU_TUC');
                      $chaythutuc = $this->db->Execute($stmt);
                      if($P_VAL != 'THANH_CONG'){
                           $this->popup_exec_fail(__('Không cập nhật, vui lòng liên hệ VNPT Tiền Giang: 0949291288!'));
                           return;
                      }
                  }
              }
               $this->popup_exec_done();

          }else{
               $this->popup_exec_fail(__('Không thể phân quyền!'));
               return;
          }
        }

     }

}